﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Printing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SoonCode.FocusTest.Utils
{
    class PrintUtil
    {
        private static string[] printerList = { "POS", "RP80" };

        public void Print(UserControl userControl, bool rorate90, int copyCount)
        {
            //旋转
            if (rorate90)
            {
                var rotateTransform = new RotateTransform(90);
                userControl.LayoutTransform = rotateTransform;
            }

            //刷新UI
            var size = new Size(userControl.Width, userControl.Height);
            userControl.Measure(size);
            userControl.Arrange(new Rect(new Point(0, 0), size));

            //设置对齐方式
            userControl.HorizontalAlignment = HorizontalAlignment.Left;
            userControl.VerticalAlignment = VerticalAlignment.Top;

            //开始打印
            Print(userControl, "test print", copyCount);
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="userControl">要打印的控件</param>
        /// <param name="printer">打印机名称</param>
        /// <param name="description">打印描述</param>
        /// <param name="copyCount">打印个数</param>
        public static void Print(UserControl userControl, string description, int copyCount)
        {
            string printer = GetPrinter();
            if (printer == "")
            {
                throw new Exception("没有找到合适的打印机，请检查设备打印机是否正常！");
            }

            var localPrintServer = new LocalPrintServer();
            var printQueue = localPrintServer.GetPrintQueue(printer);
            if (printQueue.IsInError)
            {
                throw new Exception("打印机处于错误状态");
            }

            var printDialog = new PrintDialog
            {
                PrintQueue = printQueue, //打印队列
                PrintTicket = { CopyCount = copyCount } //打印个数
            };

            //设置纸张大小
            var pageWidth = (int)Math.Ceiling(printDialog.PrintableAreaWidth); //小标签：114
            var pageHeight = (int)Math.Ceiling(printDialog.PrintableAreaHeight); //小标签：227
            printDialog.PrintTicket.PageMediaSize = new PageMediaSize(pageWidth, pageHeight);

            //打印
            printDialog.PrintVisual(userControl, description);
        }

        public static string GetPrinter()
        {
            var printServer = new PrintServer();
            var PrinterList = printServer.GetPrintQueues();
            string printName = "";
            foreach (PrintQueue queue in PrinterList)
            {
                printName = GetPrinterName(queue);
                if (printName != "")
                    break;
            }

            return printName;
        }

        private static string GetPrinterName(PrintQueue queue)
        {
            foreach (string str in printerList)
            {
                if (queue.Name.Contains(str))
                {
                    return queue.Name;
                }
            }

            return "";
        }
    }
}